﻿$(document).on("pageInit", "#create-attendence", function (e, id, page) {
    loadList();

    function loadList() {

        var $clazzIdSelector = $(page).find('[name="ClazzId"]');

        $.getJSON("/Evaluate/GetAllClazzEntity", {}, function (response) {

            $clazzIdSelector.empty();
            $clazzIdSelector.append("<option>选择班级</option>");
            $.each(response, function (rIndex, rItem) {
                $clazzIdSelector.append("<option value='" + rItem.ClazzId + "'>" + rItem.ClazzName + "</option>");
            });
        });

        $clazzIdSelector.on("change", function () {
            loadStudent($clazzIdSelector.val());
        });
        function loadStudent(clazzId) {
            $.getJSON("/Evaluate/GetStudentListByClazzId", { clazzId: clazzId }, function (response) {
                var $stuList = $(page).find('#stu-list');
                $stuList.empty();

                $.each(response, function (rIndex, rItem) {
                    $stuList.append('<li class="item-content" data-stuid="' + rItem.UserId + '">\
                    <div class="item-media"><i class="icon icon-f7"></i></div>\
                    <div class="item-inner">\
                        <div class="item-title label">' + rItem.RealName + '【学号：' + rItem.StuNo + '】</div>\
                        <div class="item-input">\
                            <select name="Status">\
                                <option value="1">出勤</option>\
                                <option value="2">缺勤</option>\
                                <option value="3">迟到</option>\
                                <option value="4">旷课</option>\
                                <option value="5">早退</option>\
                            </select>\
                        </div>\
                    </div>\
                </li>');
                });
            });
        }
    }

    $(page).find("[name=save-attendance-form]").on("submit", function (evt) {
        evt.preventDefault();

        var students = [];
        $('[data-stuid]').each(function (rIndex, rItem) {
            students.push({ StudentUserId: rItem.dataset.stuid, Status: $(rItem).find('[name=Status]').val() });
        });
        utils.$postJson(students, "/Attendance/SaveAttendance?clazzId=" + $(page).find("[name=ClazzId]").val(), function (response) {
            if (response.indexOf("err:") > -1) {
                $.toast(response.substr(4));
                return false;
            }
            $.toast("保存成功！");
            history.back();
        });
    });
});